package com.yupi.yupicturebackend.manager.websocket.ws;

import com.yupi.yupicturebackend.manager.websocket.PictureEditHandler;
import com.yupi.yupicturebackend.manager.websocket.model.PictureEditActionEnum;
import com.yupi.yupicturebackend.manager.websocket.model.PictureEditMessageTypeEnum;
import com.yupi.yupicturebackend.manager.websocket.model.PictureEditRequestMessage;
import com.yupi.yupicturebackend.manager.websocket.model.PictureEditResponseMessage;
import com.yupi.yupicturebackend.model.entity.User;
import org.springframework.stereotype.Component;
import org.springframework.web.socket.WebSocketSession;

@Component("EDIT_ACTION")
public class EditActionHandler extends BaseMessageHandler {
    @Override
    public void handle(PictureEditRequestMessage request,
                       WebSocketSession session,
                       User user,
                       Long pictureId,
                       PictureEditHandler context) throws Exception {
        Long editUserId = context.getPictureEditingUsers().get(pictureId);
        String editAction = request.getEditAction();
        PictureEditActionEnum actionEnum = PictureEditActionEnum.getEnumByValue(editAction);

        if (actionEnum != null && editUserId != null && editUserId.equals(user.getId())) {
            PictureEditResponseMessage response = new PictureEditResponseMessage();
//            response.setType(PictureEditMessageTypeEnum.EDIT_ACTION.getValue());
            // 这里本来是EDIT_ACTION，但是为了兼容前端，这里改为ENTER_EDIT
            response.setType(PictureEditMessageTypeEnum.ENTER_EDIT.getValue());
            response.setMessage(String.format("%s执行%s", user.getUserName(), actionEnum.getText()));
            response.setUser(userService.getUserVO(user));
            response.setEditAction(editAction);

            broadcastToPicture(pictureId, response, session, context);
        }
    }
}